package com.jyxd.web.controller.zauto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyxd.web.data.zauto.ZAutoAdviceTaskEntity;
import com.jyxd.web.data.zauto.form.ZAutoAdviceQueryForm;
import com.jyxd.web.data.zauto.form.ZAutoAssociatedAdviceForm;
import com.jyxd.web.data.zauto.form.ZautoAdviceAddForm;
import com.jyxd.web.service.zauto.ZAutoAdviceTaskService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.JsonArrayValueProcessor;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/autoAdvice")
public class ZAutoAdviceController {

    @Resource
    private ZAutoAdviceTaskService zAutoAdviceTaskService;

    /**
     * 分页查询
     *
     * @param zAutoAdviceQueryForm
     * @return
     */
    @PostMapping("/getList")
    public String getList(@RequestBody ZAutoAdviceQueryForm zAutoAdviceQueryForm) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        Page<ZAutoAdviceTaskEntity> result = zAutoAdviceTaskService.getList(zAutoAdviceQueryForm);

        if (result.getRecords() != null && !result.getRecords().isEmpty()) {
            json.put("msg", "查询成功");

            List<ZautoAdviceAddForm> collect = result.getRecords().stream().map(
                    zAutoAdviceTaskEntity -> {
                        ZautoAdviceAddForm zautoAdviceAddForm = new ZautoAdviceAddForm();
                        BeanUtils.copyProperties(zAutoAdviceTaskEntity, zautoAdviceAddForm);
                        List<String> strings = JSONArray.parseArray(zAutoAdviceTaskEntity.getAdviceIdJson(), String.class);
                        zautoAdviceAddForm.setAdviceIdList(strings);
                        return zautoAdviceAddForm;
                    }
            ).collect(Collectors.toList());
            json.put("data", collect);
        }
        json.put("totalCount", result.getRecords().size());
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    @GetMapping("/adviceList/{patientId}/{adviceType}")
    public String adviceList(@PathVariable("patientId") String patientId, @PathVariable("adviceType") String adviceType) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        List<ZAutoAssociatedAdviceForm> list = zAutoAdviceTaskService.adviceList(patientId, adviceType);
        if (list != null) {
            json.put("msg", "查询成功");
            json.put("data", list);
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }


    @PostMapping("/add")
    public String add(@RequestBody ZautoAdviceAddForm zautoAdviceAddForm) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        List<String> adviceIdList = zautoAdviceAddForm.getAdviceIdList();
        String adviceIdJson = JSON.toJSONString(adviceIdList);

        zautoAdviceAddForm.setAdviceIdJson(adviceIdJson);
        zAutoAdviceTaskService.save(zautoAdviceAddForm);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "添加成功");
        return json.toString();
    }

    @PostMapping("/update")
    public String update(@RequestBody ZautoAdviceAddForm zautoAdviceAddForm) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        List<String> adviceIdList = zautoAdviceAddForm.getAdviceIdList();
        String adviceIdJson = JSON.toJSONString(adviceIdList);
        zautoAdviceAddForm.setAdviceIdJson(adviceIdJson);
        zAutoAdviceTaskService.updateById(zautoAdviceAddForm);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "修改成功");
        return json.toString();
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        zAutoAdviceTaskService.removeById(id);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "删除成功");
        return json.toString();
    }


    @GetMapping("/disabled/{id}/{disabled}")
    public String disabled(@PathVariable("id") Long id, @PathVariable("disabled") Integer disabled) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        LambdaUpdateWrapper<ZAutoAdviceTaskEntity> zAutoAdviceTaskEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        zAutoAdviceTaskEntityLambdaUpdateWrapper.eq(ZAutoAdviceTaskEntity::getId, id);
        if (disabled == 1){
            zAutoAdviceTaskEntityLambdaUpdateWrapper.set(ZAutoAdviceTaskEntity::getDisabled, 0);
        } else if (disabled == 0) {
            zAutoAdviceTaskEntityLambdaUpdateWrapper.set(ZAutoAdviceTaskEntity::getDisabled, 1);
        }else {
            json.put("code", HttpCode.FAILURE_CODE.getCode());
            json.put("msg", "参数错误");
            return json.toString();
        }
        zAutoAdviceTaskService.update(zAutoAdviceTaskEntityLambdaUpdateWrapper);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "修改状态成功");
        return json.toString();
    }
}
